using System;
using System.Reflection;
using UnityEngine;
using System.Collections.Generic;

namespace UIFramework
{
    public class UIUtility
    {
        private static Dictionary<string, Type> _uiTypes = null;

        private static void Init()
        {
            if (_uiTypes == null)
            {
                _uiTypes = new Dictionary<string, Type>();

                Type type = typeof(UIBase);
                Assembly ass = Assembly.GetAssembly(type);
                var types = ass.GetTypes();
                for (int i = 0; i < types.Length; i++)
                {
                    if (types[i].IsSubclassOf(type) && types[i].BaseType != type)
                    {
                        _uiTypes.Add(types[i].Name, types[i]);
                    }
                }
            }
        }

        public static UIBase GetUIClass(string name)
        {
            Init();
            UIBase bUI = null;
            if (_uiTypes.ContainsKey(name))
            {
                object o = Activator.CreateInstance(_uiTypes[name], true);
                bUI = o as UIBase;
            }
            else
            {
                Debug.LogError("Can't find ui type with name :" + name);
            }
            return bUI;
        }

        public static string GetSortLayerName(EUICanvas canvas)
        {
            switch (canvas)
            {
                case EUICanvas.Middle:
                    return "UI_Middle";
                case EUICanvas.Top:
                    return "UI_Top";
                default:
                    return "UI_Bottom";
            }
        }
    }
}
